package com.wq.data.redpacket.repository;

import com.wq.api.domain.entity.capital.TradeOrder;
import com.wq.api.repository.capital.TradeOrderRepository;
import com.wq.api.repository.redpacket.RedPacketTradeOrderRepository;
import com.wq.data.capital.dao.TradeOrderMapper;
import com.wq.data.redpacket.dao.RedPacketTradeOrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.stereotype.Repository;

/**
 * Created by twinkle.zhou on 16/11/14.
 */
@Repository
@DubboService
public class RedPacketTradeOrderRepositoryImpl implements RedPacketTradeOrderRepository {

    @Autowired
    RedPacketTradeOrderMapper redPacketTradeOrderMapper;

    @Override
    public void insert(TradeOrder tradeOrder) {
        redPacketTradeOrderMapper.insert(tradeOrder);
    }

    @Override
    public void update(TradeOrder tradeOrder) {
        tradeOrder.updateVersion();
        int effectCount = redPacketTradeOrderMapper.update(tradeOrder);
        if (effectCount < 1) {
            throw new OptimisticLockingFailureException("update trade order failed");
        }
    }

    @Override
    public TradeOrder findByMerchantOrderNo(String merchantOrderNo) {
        return redPacketTradeOrderMapper.findByMerchantOrderNo(merchantOrderNo);
    }

}
